﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace Dao
{
    public class KC_WLPD_Dao
    {
        public List<vWLKC> QueryBySql(string bh, string mc, string ckid, string cwid)
        {
            List<vWLKC> list = new List<vWLKC>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.vWLKCSet
                        where c.BH.Contains(bh)
                        select c;

                if (!string.IsNullOrEmpty(mc))
                {
                    q = q.Where(c => c.MC.Contains(mc));
                }

                if (!string.IsNullOrEmpty(ckid) && (!"0".Equals(ckid)))
                {
                    Guid cid = Guid.Parse(ckid);
                    q = q.Where(c => c.CKID == cid);
                }

                if (!string.IsNullOrEmpty(cwid) && (!"0".Equals(cwid)))
                {
                    Guid cwGuid = Guid.Parse(cwid);
                    q = q.Where(c => c.CWID == cwGuid);
                }

                foreach (vWLKC c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }

        public bool Revise(Guid userId, Guid departmentId, List<vWLKCEx> list)
        {
            if (list == null || list.Count == 0)
            {
                return true;
            }

            bool res = false;

            using (var context = new Dao.ErpEntities())
            {
                List<Guid> ids = new List<Guid>();
                List<Guid> wpids = new List<Guid>();
                foreach (var item in list)
                {
                    ids.Add(item.ID);
                    wpids.Add(item.WPID);
                }

                // 取要修订的物料库存，如果有则修改库存数量
                var q = from kc in context.KCSet
                        where ids.Contains(kc.ID)
                        select kc;

                foreach (var kc in q)
                {
                    kc.SL = list.FirstOrDefault(c => c.ID == kc.ID).SL;
                    ids.Remove(kc.ID);
                    context.KCSet.ApplyCurrentValues(kc);
                }


                // 取要修订的物料，如果有则修改安全库存数量
                var q2 = from wl in context.ZL_WLSet
                        where wpids.Contains(wl.ID)
                        select wl;

                foreach (var wl in q2)
                {
                    wl.AQKCSL = (double)(list.FirstOrDefault(c => c.WPID == wl.ID).AQKCSL);
                    wpids.Remove(wl.ID);
                    context.ZL_WLSet.ApplyCurrentValues(wl);
                }

                context.JCCDSet.AddObject(createHistory(userId, departmentId, list));

                res = context.SaveChanges() >= 1;
            }

            return res;
        }

        private JCCD createHistory(Guid userId, Guid departmentId, List<vWLKCEx> list)
        {
            JCCD jccd = new JCCD()
            {
                ID = Guid.NewGuid(),
                BH = "WLPD" + DateTime.Now.ToString("yyyyMMddHHmmss"),
                LX = "9999",
                BMID = departmentId,
                RYID = userId,
                RQ = DateTime.Now,
                CGYID = userId,
                ZT = 1
            };

            foreach(vWLKCEx kc in list)
            {
                JCCDMXB mxb = new JCCDMXB()
                {
                    ID = Guid.NewGuid(),
                    WPID = kc.WPID,
                    WPLX = "01",
                    CKID = kc.CKID,
                    CWID = kc.CWID,
                    DJLX = "9999",
                    SL = kc.SL,
                    DWBH = kc.DWBH,
                    DJ = (double)kc.ZXDJ,
                    JCSJ = DateTime.Now,
                    JCCDID = jccd.ID
                };

                jccd.JCCDMXB.Add(mxb);
            }

            return jccd;
        }
    }
}
